﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Facebook;
using System.IO;

namespace DemoFacebookAPI
{
    class FBHelper
    {
        public static string GetUserID(string accessToken)
        {
            FacebookClient fb = new FacebookClient(accessToken);

            dynamic myData = fb.Get("/me");
            return myData.id;
        }

        public static string GetUserName(string accessToken)
        {
            FacebookClient fb = new FacebookClient(accessToken);
            dynamic myData = fb.Get("/me");
            return myData.name;
        }

        public static Owner GetUser(string accessToken)
        {
            FacebookClient fb = new FacebookClient(accessToken);
            dynamic myData = fb.Get("/me");
            if (myData != null)
            {
                Owner owner = new Owner();
                //get name
                owner.Name = myData.name;

                //get school
                dynamic schoolList = myData.education;
                for (int i = 0; i < myData.education.Count; i++)
                {
                    if (schoolList[i].type == "College")
                    {
                        owner.School = schoolList[i].school.name;
                        break;
                    }
                }

                //get live
                //owner.Live = myData.location.name;

                //get Link
                owner.Link = myData.link;
                return owner;
            }
            return null;
        }
        
        public static bool Post(string accessToken, string _status, string _link = "")
        {
            try
            {
                FacebookClient fb = new FacebookClient(accessToken);
                //Dictionary<string, object> postArgs = new Dictionary<string, object>();
                //postArgs["message"] = _status;

                //if (_link != "")
                //{
                //    postArgs["link"] = _link;
                //}

                fb.Post("/me/feed", new { message = _status,link = _link});
                return true;
            }
           catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                return false;
            }
        }

        public static bool PostImage(string accessToken, string status, string imagePath)
        {
            try
            {
                FacebookClient fb = new FacebookClient(accessToken);

                var imgStream = File.OpenRead(imagePath);

                fb.Post("/me/photos", new
                {
                    message = status,

                    file = new FacebookMediaStream
                    {
                        ContentType = "image/jps",
                        FileName = Path.GetFileName(imagePath)
                    }.SetValue(imgStream)
                });
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        internal static bool PostStatus(string accessToken, string _status)
        {
            try
            {
                FacebookClient fb = new FacebookClient(accessToken);

                fb.Post("/me/feed", new { message = _status });
                return true;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                return false;
            }
        }
    }
}
